<?php
$this->headTitle('Tin Rao Vặt','append');
?>
<div class="dashboard-menu dashboard-admin">
    <span class="back">&nbsp;</span>
    <a href="/admin/product"><h1>Tin Rao Vặt</h1></a>
    <span class="text">Thêm</span>
</div>
<div class="row placeholders">
    <div class="col-md-12">
        <div class="widget-big">
            <div class="search-result text-left">
                <?php
                    if(isset($this->mess)){
                        if($this->flag == 'success')
                            echo '<div class="bg-success">',$this->mess,'</div>';
                        elseif($this->flag == 'alert')
                            echo '<div class="bg-warning">',$this->mess,'</div>';
                        else
                            echo '<div class="bg-danger">',$this->mess,'</div>';
                    }
                    echo $this->form; 
                ?>
            </div>
        </div>
    </div>
</div>
<?php
    $configPrice = new Zend_Config_Ini(CONFIG_PATH.'/price.ini', APPLICATION_ENV);
    $price=$configPrice->price->toArray();    
?>
<script type="text/javascript">           			
    function daydiff(first, second) {
    	return (second-first)/(1000*60*60*24)		
	}
    function numberWithCommas(x) {
        x = x.toString(); var pattern = /(-?\d+)(\d{3})/; while (pattern.test(x))
        x = x.replace(pattern, "$1,$2"); return x;
    }
    function readURL(input,id) {                
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('img#'+id).attr('src',e.target.result);                                
            }
            reader.readAsDataURL(input.files[0]);
        }
    }	  
    $(document).ready(function(){
        //Generate Title
        var transaction = $("select#transaction :selected").text();
        var itemName = $("select#itemId :selected").text();
        $('input[type=text][name=title]').val(transaction+' '+itemName);
        //setDisplay elementDate
        var d = new Date();                    
        $("input#createDate").val( formatDate(d) );                        
        if($("select#type :selected").val() == 'no'){
            $("dt#startDate-label").attr('style','display:none');
            $("input#startDate").attr('style','display:none');
            $("dt#endDate-label").attr('style','display:none');
            $("input#endDate").attr('style','display:none');
            $("select#userId").val(0);
            $("dt#priceAd-label").attr('style','display:none');
            $("input#priceAd").attr('style','display:none');
        }else{
            $("dt#startDate-label").attr('style','');
            $("input#startDate").attr('style','');
            $("dt#endDate-label").attr('style','');
            $("input#endDate").attr('style','');
            $("dt#priceAd-label").attr('style','');
            $("input#priceAd").attr('style','');
        }        
    });                          
    $(document).ready(function(){
        $("input[type=file]").each(function(){
            if($(this).attr('id') != 'photo-0' ){
                $(this).attr('style','display:none');
            }                        
        }); 
        $("input[type=file]").change(function(){            
            var id = $(this).attr('id');
            var id = id.split('-');     var ime = "photo-"+id[1];        
            var id = parseInt(id[1])+1;                                    
            $("input#photo-"+id).attr('style','');                                                
            if( $('img#'+ime).length ){
                readURL(this,ime);
            }else{                
                $(this).before(
                    '<img id="'+ime+'" src="" style="width:100px;height:100px"  onerror="this.onerror=null;this.src=\'/static/images/noimage.jpg\';"/>'
                );
                readURL(this,ime);
            }                                                                                                                        
        });               
        $('#submitbutton').click(function(){            
            if($("select#type :selected").val() == 'vi'){                
                if($('input#priceAd').val() == ''){
                    alert("Ngày kết thúc bài đăng phải nhỏ hơn ngày bắt đầu !!");
                    return false;
                }
                if($('select#userId :selected').val() == '0'){
                    alert("Cần chọn tài khoản sở hữu cho loại tin VIP!!");
                    return false;
                }
            }                                                                                
        }); 
        $("select#itemId").change(function(){            
            if($("select#transaction :selected").val()!='or'){
                var transaction = $("select#transaction :selected").text();
                var itemName = $("select#itemId :selected").text();
                $('input[type=text][name=title]').val(transaction+' '+itemName);    
            }else{
                var itemName = $("select#itemId :selected").text();
                $('input[type=text][name=title]').val(itemName);
            }                                    
        });            
        $("select#transaction").change(function(){            
            if($("select#transaction :selected").val()!='or'){
                var transaction = $("select#transaction :selected").text();
                var itemName = $("select#itemId :selected").text();
                $('input[type=text][name=title]').val(transaction+' '+itemName);    
            }else{
                var itemName = $("select#itemId :selected").text();
                $('input[type=text][name=title]').val(itemName);
            }                                              
        });                        
        $("input[type=text][name=endDate]").change(function(){            
            var start = $("input[type=text][name=startDate]").val();                         
            var end = $("input[type=text][name=endDate]").val();
            var tempDate = start.split(" ");var start = tempDate[0].split("-");
            var tempDate = end.split(" ");var end = tempDate[0].split("-");                                    
            var start = new Date((start[0]) , (start[1]-1), (start[2]));
            var end = new Date((end[0]) , (end[1]-1), (end[2]));
            var numDate = daydiff(start, end);
            if(numDate < 0){
                alert("Ngày kết thúc bài đăng phải nhỏ hơn ngày bắt đầu !!");
                $("input[name=priceAd]").val('');                
                return false;
            }else{
                var price = numberWithCommas((numDate+1)*<?php echo $price['price'] ?>);                       
                $("input[name=priceAd]").val(price+' VNĐ');
            }                                   
        });
        $("input[type=text][name=startDate]").change(function(){
            var start = $("input[type=text][name=startDate]").val();                         
            var end = $("input[type=text][name=endDate]").val();
            var tempDate = start.split(" ");var start = tempDate[0].split("-");
            var tempDate = end.split(" ");var end = tempDate[0].split("-");                                    
            var start = new Date((start[0]) , (start[1]-1), (start[2]));
            var end = new Date((end[0]) , (end[1]-1), (end[2]));
            var numDate = daydiff(start, end);
            if(numDate < 0){                
                alert("Ngày kết thúc bài đăng phải nhỏ hơn ngày bắt đầu !!");
                $("input[name=priceAd]").val('');                                
                return false;
            }else{
                var price =  numberWithCommas((numDate+1)*<?php echo $price['price'] ?>);                       
                $('input[name=priceAd]').val(price+' VNĐ');
            }           
        }); 
        $("select#type").change(function(){            
            var val = $("select#type :selected").val();
            if(val == 'no'){
                $("dt#startDate-label").attr('style','display:none');
                $("input#startDate").attr('style','display:none');
                $("dt#endDate-label").attr('style','display:none');
                $("input#endDate").attr('style','display:none');
                $("select#userId").val(0);
                $("dt#priceAd-label").attr('style','display:none');
                $("input#priceAd").attr('style','display:none');
            }else{
                $("dt#startDate-label").attr('style','');
                $("input#startDate").attr('style','');
                $("dt#endDate-label").attr('style','');
                $("input#endDate").attr('style','');
                $("dt#priceAd-label").attr('style','');
                $("input#priceAd").attr('style','');
            }            
        });
        $("select#userId").change(function(){
            var val = $("select#type :selected").val();
            if(val == 'no'){
                $("select#userId").val(0);
            }      
        });            
    });    				
</script>